﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Converters;

namespace StripeOne.ShortenUrl.API.Common
{
    public class StripeOneClassInterfaceConverter<TInterface, TClass> : CustomCreationConverter<TInterface>

        where TClass : TInterface, new()
    {
        public override TInterface Create(Type objectType)
        {
            return new TClass();
        }
    }

    public class ShortenClassSerializer
    {
        public static JsonConverter[] GetConverters()
        {
            List<JsonConverter> converters = new List<JsonConverter>();
            //converters.Add(new StripeOneClassInterfaceConverter<IUrlShorten, UrlShorten>());
            return converters.ToArray();
        }

        public static String Serialize(Object data)
        {
            return new JavaScriptSerializer().Serialize(data);
        }

        public static String Serialize(Object data, bool ignoreNullvalues)
        {
            if (ignoreNullvalues)
                return JsonConvert.SerializeObject(data,
                        Formatting.None,
                        new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });

            return Serialize(data);
        }

        public static T Deserialize<T>(String json)
        {
            JsonConverter[] converters = GetConverters();
            return JsonConvert.DeserializeObject<T>(json, converters);
        }
    }
}
